﻿using System;
using System.IO;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace CodeSmith
{
    public partial class MainForm : Form
    {
        #region 变量
        private readonly string filePath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");

        private LeftForm leftForm = null;
        private BottomForm bottomForm = null;
        private LinkData linkData = null;
        #endregion

        #region 事件委托
        public delegate void logDelegate(string text);
        public event logDelegate logEvent;
        #endregion

        #region 初始化
        public MainForm()
        {
            InitializeComponent();

            leftForm = new LeftForm();
            bottomForm = new BottomForm(this, leftForm);
            linkData = new LinkData();
        }
        #endregion

        #region 加载
        private void MainForm_Load(object sender, EventArgs e)
        {
            logEvent("初始化...");
            DeserializeDockContent ddc = new DeserializeDockContent(GetContentFromPersistString);
            if (File.Exists(filePath))
                this.mainDockPanel.LoadFromXml(filePath, ddc);
            else
            {
                leftForm.Owner = this;
                leftForm.Show(this.mainDockPanel);
                leftForm.DockTo(this.mainDockPanel, DockStyle.Left);
                bottomForm.Owner = this;
                bottomForm.Show(this.mainDockPanel);
                bottomForm.DockTo(this.mainDockPanel, DockStyle.Bottom);
            }
            linkData.ShowDialog(this);
        }

        private IDockContent GetContentFromPersistString(string persistString)
        {
            if (persistString == typeof(LeftForm).ToString())
                return leftForm;
            else if (persistString == typeof(BottomForm).ToString())
                return bottomForm;
            else
                return null;
        }
        #endregion

        #region 关闭
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.mainDockPanel.SaveAsXml(filePath);
        }
        #endregion

        #region 退出
        private void tsbExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        #endregion
    }
}
